package com.simple.action.fee;

import java.util.ArrayList;
import java.util.List;

import com.simple.po.FeeDetail;
import com.simple.po.FeeReport;
import com.xunj.action.common.AbstractAction;
import com.xunj.core.ParaUnit;
import com.xunj.service.system.RelationDataService;
import com.xunj.util.Common;

/**
 * 费用更正
 * @author 王瑾
 *
 */
public class FeeUpdateAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -365972686059686745L;
	private FeeDetail feeDetail; //费用登记
	private FeeReport feeReport; //费用登记


	



	/**
	 * 查询已上报或已核销的上报费用信息进行更正
	 * @return
	 * @throws Exception
	 */
	public String listFeeUpdate() throws Exception
	{
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();

		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		
		if(POST())
		{
			if(!feeReport.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId", feeReport.getAreaId(), ParaUnit.EQ));
			if(!feeReport.getAreaDeptId().equals(""))
				paralist.add(new ParaUnit("areaDeptId", feeReport.getAreaDeptId(), ParaUnit.EQ));
			
			if(!feeReport.getFeeReportYear().equals(""))
				paralist.add(new ParaUnit("feeReportYear", feeReport.getFeeReportYear(), ParaUnit.EQ));
			if(!feeReport.getFeeReportMonth().equals(""))
				paralist.add(new ParaUnit("feeReportMonth", feeReport.getFeeReportMonth(), ParaUnit.EQ));
			if(!feeReport.getReportState().equals(""))
				paralist.add(new ParaUnit("reportState", feeReport.getReportState(), ParaUnit.EQ));
			else{
				List orList = new ArrayList();
				orList.add(new ParaUnit("reportState","核销完毕", ParaUnit.EQ));
				orList.add(new ParaUnit("reportState","已报销", ParaUnit.EQ));
				paralist.add(new ParaUnit("", orList, ParaUnit.OR));
			}
			if(!feeReport.getFeeReportCode().equals(""))
				paralist.add(new ParaUnit("feeReportCode", feeReport.getFeeReportCode(), ParaUnit.LIKE));
			
		}
		else
		{
			List orList = new ArrayList();
			orList.add(new ParaUnit("reportState","核销完毕", ParaUnit.EQ));
			orList.add(new ParaUnit("reportState","已报销", ParaUnit.EQ));
			paralist.add(new ParaUnit("", orList, ParaUnit.OR));
		}
		
		List list = dao.criteriaByPage(FeeReport.class, paralist);

		request.setAttribute("datalist", list);
		return toJsp("/jsp/fee/fee_update/lst_fee_report.jsp");
	}
	/**
	 * 查询费用信息
	 * @return
	 * @throws Exception
	 */
	public String listDetailFeeUpdate() throws Exception
	{
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();

		paralist.add(new ParaUnit("state", "核销完毕", ParaUnit.EQ));
		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		
		if(POST())
		{
			if(!feeDetail.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId", feeDetail.getAreaId(), ParaUnit.EQ));
			if(!feeDetail.getAreaDeptId().equals(""))
				paralist.add(new ParaUnit("areaDeptId", feeDetail.getAreaDeptId(), ParaUnit.EQ));
			String feeDateFrom = request.getParameter("feeDateFrom");
			String feeDateTo = request.getParameter("feeDateTo");
			if(!feeDateFrom.equals(""))
			{
				paralist.add(new ParaUnit("feeDate",Common.newDate(feeDateFrom+" 00:00:00", "yyyy-MM-dd"), ParaUnit.GE));
				request.setAttribute("feeDateFrom", feeDateFrom);
			}
			if(!feeDateTo.equals(""))
			{
				paralist.add(new ParaUnit("feeDate",Common.newDate(feeDateTo+" 23:59:59", "yyyy-MM-dd"), ParaUnit.LE));
				request.setAttribute("feeDateTo", feeDateTo);
			}
			if(!feeDetail.getFeeTypeId().equals(""))
				paralist.add(new ParaUnit("feeTypeId", feeDetail.getFeeTypeId(), ParaUnit.EQ));
			if(!feeDetail.getFeeTarget().equals(""))
				paralist.add(new ParaUnit("feeTarget", feeDetail.getFeeTarget(), ParaUnit.EQ));
			if(!feeDetail.getTargetName().equals(""))
				paralist.add(new ParaUnit("targetName", feeDetail.getTargetName(), ParaUnit.EQ));
			
		}
		
		List list = dao.criteriaByPage(FeeDetail.class, paralist);
	
		List reportIds = new ArrayList();
		for(int i=0;i<list.size();i++)
		{
			FeeDetail fd = (FeeDetail) list.get(i);
			if(!reportIds.contains(fd.getFeeReportId()))
				reportIds.add(fd.getFeeReportId());
		}
		String reportId = "";
		if(!reportIds.isEmpty())
		{
			for(int i=0;i<reportIds.size();i++)
				reportId+="'"+reportIds.get(i)+"',";
		}
		if(reportId.length()>0)
		{
			reportId = reportId.substring(0, reportId.length()-1);
			String hql = " from FeeReport where feeReportId in ("+reportId+")";
			List feeReportList = dao.findAll(hql);
			request.setAttribute("feeReportList", feeReportList);
		}
		
		request.setAttribute("datalist", list);
		return toJsp("/jsp/fee/fee_update/lst_fee_detail.jsp");
	}
	/**
	 * 查看费用明细
	 * @return
	 * @throws Exception
	 */
	public String showDetailFeeUpdate() throws Exception
	{
		String feeReportId = request.getParameter("feeReportId");
		
		feeReport = (FeeReport)dao.findOne(FeeReport.class, feeReportId);
		
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
		paralist.add(new ParaUnit("feeReportId", feeReportId, ParaUnit.EQ));
		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		List list = dao.criteriaQuery(FeeDetail.class, paralist);

		request.setAttribute("datalist", list);
		return toJsp("/jsp/fee/fee_update/show_fee_report.jsp");
	}
	/**
	 * 费用更正
	 * @return
	 * @throws Exception
	 */
	public String updateFeeUpdate() throws Exception
	{
		if(POST())
		{
//			String feeReportId = request.getParameter("feeReportId");
			String feeIds = request.getParameter("feeIds");
			String feeTarget = request.getParameter("feeTarget");
			String targetName = request.getParameter("targetName");
			String targetId = request.getParameter("targetId");
			String feeTypeId = request.getParameter("feeTypeId");
			String feeTypeName = request.getParameter("feeTypeName");
			String updateFeeTarget = request.getParameter("updateFeeTarget");
			String updateFeeType = request.getParameter("updateFeeType");
			
			String[] feeId = feeIds.split(",");
			ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
//			paralist.add(new ParaUnit("feeReportId", feeReportId, ParaUnit.EQ));
			paralist.add(new ParaUnit("feeId", feeId, ParaUnit.IN));
			paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
			List list = dao.criteriaQuery(FeeDetail.class, paralist);
			
			RelationDataService rds = new RelationDataService(dao);
			
			for(int i=0;i<list.size();i++)
			{
				FeeDetail fd = (FeeDetail) list.get(i);
				//保存更正数据
				rds.saveRelationData("feeDetail", fd.getFeeId(), fd, false);
				if(updateFeeTarget!=null)
				{
					fd.setFeeTarget(feeTarget);
					fd.setTargetId(targetId);
					fd.setTargetName(targetName);
				}
				if(updateFeeType!=null)
				{
					fd.setFeeTypeId(feeTypeId);
					fd.setFeeTypeName(feeTypeName);
				}
				fd.setUpdateState("是");
			}

			
			this.Alert_Close("保存成功", "ok");
			return toMessage();
		}
		else
		{
			//数据更新调用来源，从上报汇总更新或从费用明细更新
			String updateType = request.getParameter("updateType");
			if(updateType.equals("feeReport"))
			{
				String feeReportId = request.getParameter("feeReportId");
				feeReport = (FeeReport)dao.findOne(FeeReport.class, feeReportId);
			}
			return toJsp("/jsp/fee/fee_update/update_fee_batch.jsp");
		}
	}

	/**
	 * 费用更正历史显示
	 * @return
	 * @throws Exception
	 */
	public String listHistoryFeeUpdate() throws Exception
	{
		RelationDataService rds = new RelationDataService(dao);
		String feeId = request.getParameter("feeId");
		List list = rds.getRelationDataList("feeDetail", feeId);
		request.setAttribute("datalist", list);
		return toJsp("/jsp/fee/fee_update/list_update_history.jsp");
	}
	
	public FeeDetail getFeeDetail() {
		return feeDetail;
	}
	public void setFeeDetail(FeeDetail feeDetail) {
		this.feeDetail = feeDetail;
	}

	public FeeReport getFeeReport() {
		return feeReport;
	}

	public void setFeeReport(FeeReport feeReport) {
		this.feeReport = feeReport;
	}


}